//
//  CLUIBaseViewController.m
//  CityHunter
//
//  Updated by xcq on 14-10-22.
//  Copyright (c) 2014年 CieNet. All rights reserved.
//

#import "Common.h"
#import "ConfigDefine.h"
#import "BaseNavController.h"
#import "CLUIBaseViewController.h"

#define kNavigationBtnSpaceHorizontal 5.0f
#define kNavigationBtnSpaceVertical 10.0f
#define kNavigationBtnHeight (self.navigationController.navigationBar.frame.size.height - 2 * kNavigationBtnSpaceVertical)

#define kSetBadgeBGColor     self.navigationItem.rightBarButtonItem.badgeBGColor = [UIColor redColor];
#define kSetBadgeTXColor     self.navigationItem.rightBarButtonItem.badgeTextColor = [UIColor whiteColor];
#define BADGEIMG @"tasklist_title_infotip_red"

@interface CLUIBaseViewController () <UIGestureRecognizerDelegate>

@property (nonatomic, retain) NSArray *requestList;

@property (nonatomic, retain) UIView *maskView;
//@property (nonatomic, retain) __block MBProgressHUD *progressHUD;
@property (nonatomic, retain) UITapGestureRecognizer *keyboardHideGr;

@end

@implementation CLUIBaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
 
- (void)viewDidLoad{
    [super viewDidLoad];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor whiteColor], NSForegroundColorAttributeName,
                                                                     [UIFont boldSystemFontOfSize:17.0], NSFontAttributeName,
                                                                     nil]];
}

- (void)setUp{
    [self setNavBarBgImgView:nil];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [SVProgressHUD dismiss];
}

- (void)removeRedPoint {
    [self removeNavLeftRedPoint];
}

- (void)addRedPoint {
    [self showNavBarLeftBadge:0];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
//    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
//                                                                     [UIColor whiteColor], NSForegroundColorAttributeName,
//                                                                     [UIFont boldSystemFontOfSize:17.0], NSFontAttributeName,
//                                                                     nil]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.view hideToastActivity];
    [SVProgressHUD dismiss];
}

- (void)didReceiveMemoryWarning
{
    NSLog(@"%@",[self class]);

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) dealloc
{       NSLog(@"[%@ dealloc]",[self class]);
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - set navigation
/**
 * 自定义Nav左按钮
 * @parama 左边标题
 */
- (void)showNavgationWithLeftItemWithTitle:(NSString *)title{
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnLeft setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
    [btnLeft addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    UIImage *leftImg = [UIImage imageNamed:BACKIMAGE];
    btnLeft.tag = kNavigationBarLeftBtnTag;
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -10;
    
    CGFloat height = kNavigationBtnHeight;
    CGFloat width = height * leftImg.size.width / leftImg.size.height;
    if (height/width < 0.8) {
        CGFloat scale = leftImg.size.height / leftImg.size.width;
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   height,
                                   height*scale);
    } else {
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   width,
                                   height);
    }
    
    UIButton *btnTiele = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTiele.titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
    [btnTiele setTitle:title forState:UIControlStateNormal];
    [btnTiele setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnTiele.frame  = CGRectMake(CGRectGetMaxX(btnLeft.frame)-20, CGRectGetMidY(btnLeft.frame), 40, btnLeft.frame.size.height);
    [btnTiele addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    btnTiele.titleEdgeInsets = UIEdgeInsetsMake(0,-10,0,0);
    
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
    UIBarButtonItem *leftTitleBar = [[UIBarButtonItem alloc] initWithCustomView:btnTiele];
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,leftBar,leftTitleBar, nil];
}

/**
 * 自定义Nav左按钮
 * @parama 左边标题 左边第二个标题
 */
- (void)showNavgationWithLeftItemWithTitle:(NSString *)title twoItemWithtitle:(NSString *)titleT{
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnLeft setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
    [btnLeft addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    UIImage *leftImg = [UIImage imageNamed:BACKIMAGE];
    btnLeft.tag = kNavigationBarLeftBtnTag;
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -10;
    
    CGFloat height = kNavigationBtnHeight;
    CGFloat width = height * leftImg.size.width / leftImg.size.height;
    if (height/width < 0.8) {
        CGFloat scale = leftImg.size.height / leftImg.size.width;
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   height,
                                   height*scale);
    } else {
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   width,
                                   height);
    }
    
    UIButton *btnTiele = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTiele.titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
    [btnTiele setTitle:title forState:UIControlStateNormal];
    [btnTiele setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnTiele.frame  = CGRectMake(CGRectGetMaxX(btnLeft.frame)-20, CGRectGetMidY(btnLeft.frame), 40, btnLeft.frame.size.height);
    [btnTiele addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    btnTiele.titleEdgeInsets = UIEdgeInsetsMake(0,-10,0,0);
    
    UIButton *btnTieleT = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTieleT.titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
    [btnTieleT setTitle:titleT forState:UIControlStateNormal];
    [btnTieleT setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnTieleT.frame  = CGRectMake(CGRectGetMaxX(btnTiele.frame), CGRectGetMidY(btnTiele.frame), 40, btnTiele.frame.size.height);
    [btnTieleT addTarget:self action:@selector(backRoot) forControlEvents:UIControlEventTouchUpInside];
    btnTieleT.titleEdgeInsets = UIEdgeInsetsMake(0,-30,0,0);
    
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
    UIBarButtonItem *leftTitleBar = [[UIBarButtonItem alloc] initWithCustomView:btnTiele];
    UIBarButtonItem *leftTitleBarT = [[UIBarButtonItem alloc] initWithCustomView:btnTieleT];
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,leftBar,leftTitleBar,leftTitleBarT, nil];
}

/**
 * 自定义Nav左右按钮(左右按钮可加Badge)
 * @parama 左右图标的图片
 * @parama 左右按钮的SEL
 * @parama 左右显示badge的数量
 * @parama 设置NavagationBar的颜色
 */
- (void)showNavBarWithRightImage:(UIImage *)rimage
                    LeftBtnImage:(UIImage *)limage
               andRightBtnAction:(SEL)raction
                andLeftBtnAction:(SEL)laction
                   andRightBadge:(NSInteger)rnum
                     andLeftBage:(NSInteger)lnum
                           title:(NSString *)title
                        barColor:(UIColor *)color {
    if (limage) {
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setBackgroundImage:limage forState:UIControlStateNormal];
        if (laction) {
            [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
        }
        leftBtn.tag = kNavigationBarLeftBtnTag;
        CGFloat height = kNavigationBtnHeight;
        CGFloat width = height * limage.size.width / limage.size.height;
        if (height/width < 0.8) {
            CGFloat scale = limage.size.height / limage.size.width;
            leftBtn.frame = CGRectMake(0,
                                        kNavigationBtnSpaceHorizontal,
                                        height,
                                        height*scale);
        } else {
            leftBtn.frame = CGRectMake(0,
                                        kNavigationBtnSpaceHorizontal,
                                        width,
                                        height);
        }
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
        if (lnum > 0) {
             self.navigationItem.leftBarButtonItem.badgeValue = [NSString stringWithFormat:@"%ld",lnum];
            self.navigationItem.leftBarButtonItem.badgeBGColor = [UIColor redColor];
            self.navigationItem.leftBarButtonItem.badgeTextColor = [UIColor whiteColor];

        }
        
    }
    if (rimage) {
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setBackgroundImage:rimage forState:UIControlStateNormal];
        if (raction) {
            [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
        }
        rightBtn.tag = kNavigationBarRightBtnTag;
        CGFloat height = kNavigationBtnHeight;
        CGFloat width = height * rimage.size.width / rimage.size.height;
        if (height/width < 0.8) {
            CGFloat scale = rimage.size.height / rimage.size.width;
            rightBtn.frame = CGRectMake(0,
                                        kNavigationBtnSpaceHorizontal,
                                        height,
                                        height*scale);
        } else {
            rightBtn.frame = CGRectMake(0,
                                        kNavigationBtnSpaceHorizontal,
                                        width,
                                        height);
        }
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        if (rnum > 0) {
            self.navigationItem.rightBarButtonItem.badgeValue = [NSString stringWithFormat:@"%ld",rnum];
            self.navigationItem.rightBarButtonItem.badgeBGColor = [UIColor redColor];
            self.navigationItem.rightBarButtonItem.badgeTextColor = [UIColor whiteColor];
            
        }
    }
    
    if (color) {
        [self.navigationController.navigationBar setBackgroundImage:nil
                                                      forBarMetrics:UIBarMetricsDefault];
        if ([self.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
            self.navigationController.navigationBar.barTintColor = color;
        } else {
            self.navigationController.navigationBar.tintColor = GLOBAL_BARTINT_COLOR;
        }
    }
    [self showNavBarWithTitle:title];
}

/**
 * 自定义Nav左右按钮(加提示红点)
 * @parama 左右图标的图片
 * @parama 左右按钮的SEL
 * @parama 左右是否显示红点
 */
- (void)showNavBarLeftAndRightSetByLeftImage:(UIImage *)limage
                               andRightImage:(UIImage *)rimage
                               andLeftAction:(SEL)laction
                              andRightAction:(SEL)raction
                      andleftShowRedTipPoint:(BOOL)isLeftShow
                     andRightShowRedTipPoint:(BOOL)isRightShow {
    [self showNavBarWithRightImage:rimage
                      LeftBtnImage:limage
                 andRightBtnAction:raction
                  andLeftBtnAction:laction
                     andRightBadge:0
                       andLeftBage:0
                             title:nil
                          barColor:nil];
    if (isLeftShow) {
        [self showNavBarLeftBadge:0];
        self.navigationItem.leftBarButtonItem.badgeOriginY    = -6;
        self.navigationItem.leftBarButtonItem.badgeOriginX    = (self.navigationItem.rightBarButtonItem.customView.frame.size.width - 5) ;
    }
    if (isRightShow) {
        [self showNavBarRightBadge:0];
        self.navigationItem.rightBarButtonItem.badgeOriginX   = (self.navigationItem.rightBarButtonItem.customView.frame.size.width - 5) ;
        self.navigationItem.rightBarButtonItem.badgeOriginY   = -6;
    }
}

/**
 * 自定义Nav左右按钮(左以图片的形式，右边可图片可文字)
 * @parama 右边按钮的图片或者title，取其一
 * @parama 右边按钮的SEL
 * @parama 左边按钮的SEL
 * @parama 左边按钮的图片
 */
- (void)showNavBarLeftImgAndRightTitle:(NSString*)title
                          andRightImge:(UIImage *)rImg
                        andRightAction:(SEL)raction
                         andLeftAction:(SEL)laction
                           andLeftImge:(UIImage *)img{
    if (img) {
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setBackgroundImage:img forState:UIControlStateNormal];
        leftBtn.adjustsImageWhenHighlighted = NO;
        if (laction) {
            [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
        }
        leftBtn.tag = kNavigationBarLeftBtnTag;
        CGFloat height = kNavigationBtnHeight;
        CGFloat width = height * img.size.width / img.size.height;
        if (height/width < 0.8) {
            CGFloat scale = img.size.height / img.size.width;
            leftBtn.frame = CGRectMake(0,
                                       kNavigationBtnSpaceHorizontal,
                                       height,
                                       height*scale);
        } else {
            leftBtn.frame = CGRectMake(0,
                                       kNavigationBtnSpaceHorizontal,
                                       width,
                                       height);
        }
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    }
    if (title || rImg) {
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        if (rImg) {
            [rightBtn setBackgroundImage:rImg forState:UIControlStateNormal];
            rightBtn.adjustsImageWhenHighlighted = NO;
            rightBtn.tag = kNavigationBarLeftBtnTag;
            CGFloat height = kNavigationBtnHeight;
            CGFloat width = height * rImg.size.width / rImg.size.height;
            if (height/width < 0.8) {
                CGFloat scale = rImg.size.height / rImg.size.width;
                rightBtn.frame = CGRectMake(0,
                                            kNavigationBtnSpaceHorizontal,
                                            height,
                                            height*scale);
            } else {
                rightBtn.frame = CGRectMake(0,
                                            kNavigationBtnSpaceHorizontal,
                                            width,
                                            height);
            }
        }else {
            [rightBtn setFrame:CGRectMake(15, kNavigationBtnSpaceHorizontal, 80, 30)];
            [rightBtn setTitle:title forState:UIControlStateNormal];
        }
        [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = rigthBar;
    }
    
   }
/**
 *  @brief  自定义Nav左右按钮(左以图片的形式，右边可图片可文字，且可以定义选中状态的图片)
 *
 *  @param title      右边控件的文字
 *  @param rImg       右边控件的图片
 *  @param selectImg  右边控件的选中图片
 *  @param raction    右边控件的点击事件
 *  @param laction    左边控件的点击事件
 *  @param img        左边控件的图片
 *  @param selectLimg 左边控件的选中图片
 */
- (void)showNavBarLeftImgAndRightTitle:(NSString*)title
                                  andRightImge:(UIImage *)rImg
                             andRightSeleteImg:(UIImage *)selectImg
                                andRightAction:(SEL)raction
                                 andLeftAction:(SEL)laction
                                   andLeftImge:(UIImage *)img
                      andLeftSeleteImg:(UIImage *)selectLimg {
    if (img) {
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setBackgroundImage:img forState:UIControlStateNormal];
        if (selectLimg) {
            [leftBtn setBackgroundImage:selectLimg forState:UIControlStateSelected];
        }
        leftBtn.adjustsImageWhenHighlighted = NO;
        if (laction) {
            [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
        }
        leftBtn.tag = kNavigationBarLeftBtnTag;
        CGFloat height = kNavigationBtnHeight;
        CGFloat width = height * img.size.width / img.size.height;
        if (height/width < 0.8) {
            CGFloat scale = img.size.height / img.size.width;
            leftBtn.frame = CGRectMake(0,
                                        kNavigationBtnSpaceHorizontal,
                                        height,
                                        height*scale);
        } else {
            leftBtn.frame = CGRectMake(0,
                                        kNavigationBtnSpaceHorizontal,
                                        width,
                                        height);
        }
       
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    }
    if (title || rImg) {
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        if (rImg) {
            [rightBtn setBackgroundImage:rImg forState:UIControlStateNormal];
            rightBtn.adjustsImageWhenHighlighted = NO;
            rightBtn.contentMode = UIViewContentModeScaleAspectFit;
            rightBtn.tag = kNavigationBarLeftBtnTag;
            CGFloat height = kNavigationBtnHeight;
            CGFloat width = height * rImg.size.width / rImg.size.height;
            if (height/width < 0.8) {
                CGFloat scale = rImg.size.height / rImg.size.width;
                rightBtn.frame = CGRectMake(0,
                                            kNavigationBtnSpaceHorizontal,
                                            height,
                                            height*scale);
            } else {
                rightBtn.frame = CGRectMake(0,
                                            kNavigationBtnSpaceHorizontal,
                                            width,
                                            height);
            }
            if (selectImg) {
                [rightBtn setBackgroundImage:selectImg forState:UIControlStateSelected];
            }
        }else {
            [rightBtn setFrame:CGRectMake(15, kNavigationBtnSpaceHorizontal, 80, 30)];
            [rightBtn setTitle:title forState:UIControlStateNormal];
        }
        [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = rigthBar;
    }
}

/**
 * 自定义Nav左右按钮(左以图片的形式，右边文字,或者右边没有按钮显示)
 * @parama 右边按钮的title
 * @parama 右边按钮的SEL
 * @parama 左边按钮的SEL
 * @parama 左边按钮的图片
 */
- (void)showNavBarWithRightTitle:(NSString *)rightTitle
                  andRightAction:(SEL)raction
                   andLeftAction:(SEL)laction
               andRightBtnIsShow:(BOOL) isShow{
    //左边返回为箭头，右边是文字
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
    if (laction) {
        [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
        
    }else{
        [leftBtn addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
        
    }
    UIImage *leftImg = [UIImage imageNamed:BACKIMAGE];
    leftBtn.tag = kNavigationBarLeftBtnTag;
    CGFloat height = kNavigationBtnHeight;
    CGFloat width = height * leftImg.size.width / leftImg.size.height;
    if (height/width < 0.8) {
        CGFloat scale = leftImg.size.height / leftImg.size.width;
        leftBtn.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   height,
                                   height*scale);
    } else {
        leftBtn.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   width,
                                   height);
    }    
    if (isShow) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, kNavigationBtnSpaceHorizontal, 80, 30)];
        label.text = rightTitle;
        label.textColor = [UIColor whiteColor];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setFrame:CGRectMake(15, kNavigationBtnSpaceHorizontal, 60, 30)];
        [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
        [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        self.navigationItem.rightBarButtonItem = rigthBar;
        
    }
    
}

/**
 * 自定义Nav左右按钮(左以图片的形式，右边以图片形式且双按钮)
 * @parama 右边按钮的title
 * @parama 右边按钮的SEL
 */
- (void)showNavBarWithRightOneImage:(UIImage *)rightOneImage
                   andRightTwoImage:(UIImage *)rightTwoImage
             andRightTwoSelectImage:(UIImage *)rightTwoSelectImage
                  andRightOneAction:(SEL)ractionOne
                   andRighTwoAction:(SEL)ractionTwo{
    //左边返回为箭头，右边是两个图片
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnLeft setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
    [btnLeft addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    UIImage *leftImg = [UIImage imageNamed:BACKIMAGE];
    btnLeft.tag = kNavigationBarLeftBtnTag;
    CGFloat height = kNavigationBtnHeight;
    CGFloat width = height * leftImg.size.width / leftImg.size.height;
    if (height/width < 0.8) {
        CGFloat scale = leftImg.size.height / leftImg.size.width;
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   height,
                                   height*scale);
    } else {
        btnLeft.frame = CGRectMake(0,
                                   kNavigationBtnSpaceHorizontal,
                                   width,
                                   height);
    }
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
    self.navigationItem.leftBarButtonItem = leftBar;
    
    UIButton *btnRightOne = [UIButton buttonWithType:UIButtonTypeCustom];
    UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:btnRightOne];
    [btnRightOne setBackgroundImage:rightOneImage forState:UIControlStateNormal];
    btnRightOne.adjustsImageWhenHighlighted = NO;
    btnRightOne.contentMode = UIViewContentModeScaleAspectFit;
    btnRightOne.tag = kNavigationBarLeftBtnTag;
    CGFloat heightOne = kNavigationBtnHeight;
    CGFloat widthOne = heightOne * rightOneImage.size.width / rightOneImage.size.height;
    if (heightOne/widthOne < 0.8) {
        CGFloat scale = rightOneImage.size.height / rightOneImage.size.width;
        btnRightOne.frame = CGRectMake(KScreenWidth - (KScreenWidth - heightOne - 10),
                                       kNavigationBtnSpaceHorizontal,
                                       heightOne,
                                       heightOne*scale);
    } else {
        btnRightOne.frame = CGRectMake(KScreenWidth - (KScreenWidth - widthOne ),
                                       kNavigationBtnSpaceHorizontal,
                                       widthOne,
                                       heightOne);
    }
    [btnRightOne setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnRightOne addTarget:self action:ractionOne forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btnRightTwo = [UIButton buttonWithType:UIButtonTypeCustom];
    UIBarButtonItem *rigthBarTwo = [[UIBarButtonItem alloc] initWithCustomView:btnRightTwo];
    [btnRightTwo setBackgroundImage:rightTwoImage forState:UIControlStateNormal];
    [btnRightTwo setBackgroundImage:rightTwoSelectImage forState:UIControlStateSelected];
    btnRightTwo.adjustsImageWhenHighlighted = NO;
    btnRightTwo.contentMode = UIViewContentModeScaleAspectFit;
    btnRightTwo.tag = kNavigationBarLeftBtnTag;
    CGFloat heightTwo = kNavigationBtnHeight;
    CGFloat widthTwo = heightTwo * rightTwoImage.size.width / rightTwoImage.size.height;
    if (heightTwo/widthTwo < 0.8) {
        CGFloat scale = rightTwoImage.size.height / rightTwoImage.size.width;
        btnRightTwo.frame = CGRectMake(KScreenWidth - (KScreenWidth - heightTwo - heightOne - 10),
                                       kNavigationBtnSpaceHorizontal,
                                       heightTwo,
                                       heightTwo*scale);
    } else {
        btnRightTwo.frame = CGRectMake(KScreenWidth - (KScreenWidth - widthTwo - widthOne-10),
                                       kNavigationBtnSpaceHorizontal,
                                       widthTwo,
                                       heightTwo);
    }
    [btnRightTwo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnRightTwo addTarget:self action:ractionTwo forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *negativeSpacerRight = [[UIBarButtonItem alloc]
                                            initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                            target:nil action:nil];
    negativeSpacerRight.width = -5;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacerRight,rigthBar,rigthBarTwo, nil];

}


/**
 * 自定义Nav左右按钮(左以图片的形式，右边文字可设置文字大小和宽度)
 * @parama 右边按钮的title
 * @parama 右边按钮的SEL
 * @parama 左边按钮的SEL
 * @parama 左边按钮的图片
 */
- (void)showNavBarWithRightTitle:(NSString *)rightTitle
                  andRightAction:(SEL)raction
                      andLeftImg:(NSString *)imageName
                   andLeftAction:(SEL)laction
                     andFontSize:(CGFloat)size
                   andRightWidth:(CGFloat)width{
    
    //左边返回为箭头，右边是文字
    if(laction != nil){
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (imageName == nil) {
            [leftBtn setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
        } else {
            [leftBtn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        }
        if (laction) {
            [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
            
        }else{
            [leftBtn addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
            
        }
        UIImage *leftImg = [UIImage imageNamed:BACKIMAGE];
        leftBtn.tag = kNavigationBarLeftBtnTag;
        CGFloat height = kNavigationBtnHeight;
        CGFloat width = height * leftImg.size.width / leftImg.size.height;
        if (height/width < 0.8) {
            CGFloat scale = leftImg.size.height / leftImg.size.width;
            leftBtn.frame = CGRectMake(0,
                                       kNavigationBtnSpaceHorizontal,
                                       height,
                                       height*scale);
        } else {
            leftBtn.frame = CGRectMake(0,
                                       kNavigationBtnSpaceHorizontal,
                                       width,
                                       height);
        }
        UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
        self.navigationItem.leftBarButtonItem = leftBar;
    }
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:CGRectMake(35, kNavigationBtnSpaceHorizontal, width, 30)];
    [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
    [rightBtn.titleLabel setFont: [UIFont boldSystemFontOfSize:size]];
    UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    /**
     *  width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和边界间距为5pix，所以width设为-5时，间距正好调整
     *  为0；width为正数时，正好相反，相当于往左移动width数值个像素
     */
    negativeSpacer.width = -15;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, rigthBar, nil];
    //self.navigationItem.rightBarButtonItem = rigthBar;
}

/**
 * 自定义Nav左右按钮(左边默认图片，右边文字可设置文字大小和宽度)
 * @parama 右边按钮的title
 * @parama 右边按钮的SEL
 * @parama 左边按钮的SEL
 * @parama 左边按钮的图片
 */
#pragma mark - network reuqest
- (void)showNavBarWithRightTitle:(NSString *)rightTitle
                  andRightAction:(SEL)raction
                   andLeftAction:(SEL)laction
                     andFontSize:(CGFloat)size
                   andRightWidth:(CGFloat)width{
    
    //左边返回为箭头，右边是文字
    if(laction != nil){
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setBackgroundImage:[UIImage imageNamed:BACKIMAGE] forState:UIControlStateNormal];
        if (laction) {
            [leftBtn addTarget:self action:laction forControlEvents:UIControlEventTouchUpInside];
            
        }else{
            [leftBtn addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
            
        }
        UIImage *leftImg = [UIImage imageNamed:BACKIMAGE];
        leftBtn.tag = kNavigationBarLeftBtnTag;
        CGFloat height = kNavigationBtnHeight;
        CGFloat width = height * leftImg.size.width / leftImg.size.height;
        if (height/width < 0.8) {
            CGFloat scale = leftImg.size.height / leftImg.size.width;
            leftBtn.frame = CGRectMake(0,
                                       kNavigationBtnSpaceHorizontal,
                                       height,
                                       height*scale);
        } else {
            leftBtn.frame = CGRectMake(0,
                                       kNavigationBtnSpaceHorizontal,
                                       width,
                                       height);
        }
        UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
        self.navigationItem.leftBarButtonItem = leftBar;
    }
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:CGRectMake(25, kNavigationBtnSpaceHorizontal, width, 30)];
    [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:raction forControlEvents:UIControlEventTouchUpInside];
    [rightBtn.titleLabel setFont: [UIFont systemFontOfSize:size]];
    UIBarButtonItem *rigthBar = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rigthBar;
}

- (void)showNavBarLeftAndRightBadgeNum:(NSUInteger)lbadge
                              andRight:(NSUInteger)rbadge {
    [self showNavBarLeftBadge:lbadge];
    [self showNavBarRightBadge:rbadge];
}

- (void)showNavBarLeftBadge:(NSUInteger)badge {
    kSetBadgeBGColor
    kSetBadgeTXColor
    self.navigationItem.leftBarButtonItem.badgeValue = NSUINTCONVERSTR(badge);
}

- (void)showNavBarRightBadge:(NSUInteger)badge {
    kSetBadgeBGColor
    kSetBadgeTXColor
    self.navigationItem.rightBarButtonItem.badgeValue = NSUINTCONVERSTR(badge);
}

- (void)removeNavRedPoint {
    [self removeNavLeftRedPoint];
    [self removeNavRightRedPoint];
}

- (void)removeNavLeftRedPoint {
    if (self.navigationItem.leftBarButtonItem.badge) {
        [self.navigationItem.leftBarButtonItem.badge removeFromSuperview];
        self.navigationItem.leftBarButtonItem.badgeOriginX = -19;
        self.navigationItem.leftBarButtonItem.badgeOriginY = -5;
    }
}

- (void)removeNavRightRedPoint {
    if (self.navigationItem.rightBarButtonItem.badge) {
        [self.navigationItem.rightBarButtonItem.badge removeFromSuperview];
    }
}

- (void)showNavBarWithTitle:(NSString *)title {
    if (title) {
        self.title = title;
    }
}

- (void)setNavBarBgImgView:(NSString *)imageStr {
    self.navigationController.navigationBarHidden = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    if ([Common systemVersion] == ESystemVer6OrLess) {
        if (!imageStr) {
            return;
        }
    } else {
        if (!imageStr) {
            return;
        }
        self.navigationController.navigationBar.translucent = NO;
    }
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:imageStr]
                                                                 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                  forBarMetrics:UIBarMetricsDefault];
}



- (void)backTo{
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
     }
}

- (void)backRoot{
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)rightBtnAction {
    
}

#pragma mark - mask
- (void)displayMask {
    if (!self.maskView) {
        self.maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        self.maskView.backgroundColor = [UIColor clearColor];
        self.maskView.alpha = 1.f;
    }
    if (!self.maskView.superview) {
        [self.view.window addSubview:self.maskView];
    }
 }

- (void)hideMask {
     if (self.maskView) {
        [self.maskView removeFromSuperview];
    }
}

- (void)requestFailOption:(NSError*)error {
    if ([self isPresent]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[Common getLocalStr:@"NetworkRequestFail"]
                                                        message:[error.userInfo objectForKey:@"errorMsg" ]
                                                       delegate:self
                                              cancelButtonTitle:[Common getLocalStr:@"CommonTitleConfirm"]
                                              otherButtonTitles:nil, nil];
        alert.tag = kRequestFailRetBtnTag;
        [alert show];
    }
}

#pragma mark - Present
- (BOOL)isPresent {
    if (self.navigationController) {
        return self == self.navigationController.viewControllers.lastObject;
    }
    return YES;
}

#pragma mark - storyboard segua
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
}

- (IBAction)unwindSegue:(UIStoryboardSegue *)sender {
    NSLog(@"back to: %@", NSStringFromClass([self class]));
}

-(void)separatorForTableView:(UITableView *)tableView {
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    if (!tableView.tableFooterView) {
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    }
}

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

@end
